// Support file to define subroutines to send DSI frame

// include MIPI data types
# FILE "../DSIDataTypes.txt"

// Subroutine: VBlank: Outputs LineCnt vertical blanking lines with FrameDT horizontal blanking dataType
//
// Arguments:
// 	 FrameDT: HSync start data type to use for blanking line (i.e. VSS, VSE, HSS)
//   LineCnt: number of blanking lines to output
//
// Assumes the following globals are set:
// 	 HSYNC, HBP, HFP: frame timing fields in ns
//   BitsPerPixel: bits per pixel for the current format
//
# SUB VBlank FrameDT LineCnt 
	# LOOP_START LineCnt
		# HS_PACKET: FrameDT 0 0 -1
		# LP_STATES ACT HSYNC: 3
		# HS_PACKET: DT_HSYNC_END 0 0 -1
		# LP_STATES ACT HBP: 3
		# HS_PACKET_PLUS_CRC DT_BLANKING
		*(SYS_HACTIVE*BitsPerPixel/8) 0 0 0
		# LP_STATES ACT HFP: 3
	# LOOP_END
# ENDSUB

// frame timing globals
# CONST VSYNC = 10
# CONST VBP = 10
# CONST VFP = 10
# CONST HSYNC = 1000
# CONST HBP = 2000
# CONST HFP = 2000

// Subroutine: SendFrame: Outputs DSI frame 
// 
// Arguments:
//   VideoPktSub: Subroutine name (string) to call to output one active line
//                Should accept line number (zero-based) as argument
//   BitsPerPixel: BitsPerPixel setting for the current pixel format
//  
// Assumes the following globals are set:
//   VSYNC, VBP, VFP: frame timing fields in lines
// 	 HSYNC, HBP, HFP: frame timing fields in ns
//   BitsPerPixel: bits per pixel for the current format
//
// Uses SYS_HACTIVE and SYS_VACTIVE constants from current frame timing settings
//
# SUB SendFrame VideoPktSub BitsPerPixel

	// vertical sync/back porch
	# CALL VBlank DT_VSYNC_START 1
	# CALL VBlank DT_HSYNC_START (VSYNC-2)
	# CALL VBlank DT_VSYNC_END 1
	# CALL VBlank DT_HSYNC_START VBP
	
	// active lines
	# Line = 1
	# LOOP_START SYS_VACTIVE
	
		// horizontal sync
		# HS_PACKET: DT_HSYNC_START 0 0 -1
		# LP_STATES ACT HSYNC: 3
		# HS_PACKET: DT_HSYNC_END 0 0 -1

		// horizontal back porch
		# LP_STATES ACT HBP: 3
		
		// video packet
		# CALL VideoPktSub Line
	
		// horizontal front porch
		# LP_STATES ACT HBP: 3
	
		# Line = Line + 1
	# LOOP_END
	
	// vertical front porch
	# CALL VBlank DT_HSYNC_START VFP
	
# ENDSUB

